Boxplots with ggplot2

Boxplots are convenient way of graphically depicting groups of numerical data through their quartiles. Box plots may also have lines extending vertically from the boxes (whiskers) indicating variability outside the upper and lower quartiles, hence the terms box-and-whisker plot and box-and-whisker diagram. Outliers may be plotted as individual points.

Let's see how we can create them with qplot and ggplot!

In [3]:
library(ggplot2)
df <- mtcars
In [4]:
head(df)
Out[4]:
mpgcyldisphpdratwtqsecvsamgearcarb
Mazda RX42161601103.92.6216.460144
Mazda RX4 Wag2161601103.92.87517.020144
Datsun 71022.84108933.852.3218.611141
Hornet 4 Drive21.462581103.083.21519.441031
Hornet Sportabout18.783601753.153.4417.020032
Valiant18.162251052.763.4620.221031

qplot

In [10]:
qplot(factor(cyl), mpg, data = mtcars, geom = "boxplot")

ggplot

In [11]:
pl <- ggplot(mtcars, aes(factor(cyl), mpg))

pl + geom_boxplot()
In [13]:
pl + geom_boxplot() + coord_flip()
In [14]:
pl + geom_boxplot(aes(fill = factor(cyl)))
In [16]:
pl + geom_boxplot(fill = "grey", color = "blue")

That's it for the basics of creating boxplots in ggplot2!